<?php 
/**
 * LoginForm class.
 * LoginForm is the data structure for keeping
 * user login form data. It is used by the 'login' action of 'SiteController'.
 */
class MenuForm extends CFormModel
{
	public $index;
	public $id;
	public $pid;
	public $name;
	public $link;
	public $type;
	public $cname;
	public $order = 500;
	public $description;

	private $_identity;

	/**
	 * Declares the validation rules.
	 * The rules state that username and password are required,
	 * and password needs to be authenticated.
	 */
	public function rules()
	{
		return array(
			// username and password are required
			array('name, link', 'required'),
			array('index, id, pid, order', 'numerical', 'integerOnly'=>true),
			// rememberMe needs to be a boolean
			array('name, link, order, cname, description', 'safe'),

		);
	}
	
	/**
	 * Declares attribute labels.
	 */
	public function attributeLabels()
	{
		return array(
			'index' => Yii::t('common', 'Index'),
			'id' => Yii::t('common', 'Menu ID'),
			'pid' => Yii::t('common', 'Parent ID'),
			'name' => Yii::t('common','Menu Name'),
			'type' => Yii::t('common', 'Menu Type'),
			'link' => Yii::t('common','Menu Link'),
			'order' => Yii::t('common','Menu Order'),
			'cname' => Yii::t('common', 'Menu Cname'),
			'description' => Yii::t('common', 'Menu Description')
		);
	}
	
	/**
	 * Automatic Generate the menu id
	 */
	public function generateId()
	{
		$id = 0;
		$menu = $this->loadModel();
		foreach ($menu as $item)
		{
			if($item['id'] > $id)
			{
				$id = $item['id'];
			}
		}
		
		return ++$id;
		
	}
	public function getRootParent()
	{
		$parentArr = array();
		$model = $this->loadModel();
		$menu = $this->getMenuArray($model);
		
		foreach ($menu as $item)
		{
			if($item['pid'] == 0)
			{
				array_push($parentArr, $item);
			}
		}
		
		return $parentArr;
	}
	
	public function getRootParentDropdownList()
	{
		$data = array();
		$list = $this->getRootParent();
		foreach ($list as $item)
		{
			$data[$item['type']] = $item['name'];
		}
		
		return $data;
	}
	
	public function getParentCname($id)
	{
		
	}
	
	public function getChildrenArray($array,$pid=0,$y=0,&$tdata=array())
	{
		if($array){
			//然后递归的取出各个子分类，这里默认的$pid=0是因为数据库中的pid为0的表示是第一级分类
			foreach ($array as $value)
			{
				if($value['pid']==$pid){
					$n = $y + 1;
					$value['deep'] = $n;
	//				if($n > 1)
	//				{
	//					$value['cate_name']=$value['cate_name'];
	//				}
					$tdata[]=$value;
					$this->getChildrenArray($array,$value['id'],$n,$tdata);//这里递归调用，不明白递归的朋友，去找几个简单的递归例子熟悉下
				}
			}
			return $tdata;
		}
	}
	
	public function loadModel()
	{
		$model = Settings::model()->find(array(
			'condition' => 'set_key = :set_key',
			'params' => array(
				':set_key'=>'menucategory'
			)
		));
		
		if($model === null){
			$model = new Settings();
			$model->set_key = 'menucategory';
			$model->set_value = serialize(array());
			$model->save();
		}
		return $model;
	}	
	
	public function loadManualMenus()
	{
		$model = Settings::model()->find(array(
			'condition' => 'set_key = :set_key',
			'params' => array(
				':set_key'=>'menumanual'
			)
		));
		
		if($model === null){ 
			$model = new Settings();
			$model->set_key = 'menumanual';
			$model->set_value = serialize(array());
			$model->save();
		}
		return $model;
	}
	
	public function getMenuArray($model)
	{
		if($model)
			return $this->getChildrenArray(unserialize($model->set_value));
	}

	public function getMenuModelArray()
	{
		$dataProvider = $manual = $category = array();
		if($this->loadManualMenus()){
			$dataProvider = $manual = $this->getMenuArray($this->loadManualMenus());
		}
		if($this->loadModel()){
			$dataProvider = $category = $this->getMenuArray($this->loadModel());
		}
		if($this->loadManualMenus() && $this->loadModel()){
			$dataProvider = array_merge(array(),$category);
		}
		return $dataProvider;
	}
}